// #ifndef VUE3
import Vue from 'vue'
import App from './App'
import store from 'store/index.js'
import Request from "./config/TokenRequst.js"
import util from './config/util.js'
import websocket from './config/websocket.js'
import config from './config/config.js'
import uView from '@/uni_modules/uview-ui'
Vue.use(uView)
uni.$u.config.unit = 'rpx'

//监听服务器发送的消息,并同步至store中
const onMessage = (data) => {
	let message = JSON.parse(data)
	console.log("websocket收到消息:")
	console.log(message)
	if (message.messageType == 2 || message.messageType == 1) {
		store.commit("addMessage", message)
		if (message.toId == store.state.userInfo.id) {
			let data = {
				messageType: -2,
				data: message.id
			}
			ws.send(data)
		}
	} else if (message.messageType == 4 || message.messageType == 3) {
		store.commit("addGroupMessage", message)
	} else if (message.messageType == 5) {
		store.commit("friendRequest", parseInt(message.data))
	} else if (message.messageType == 6) {
		store.commit("groupRequest", parseInt(message.data))
	} else if (message.messageType == 7) {
		uni.$emit("repeat")
	} else if (message.messageType == -1) {}
}

let ws = new websocket("ws://" + config.url + "/ws", onMessage)
Vue.prototype.$socket = ws

uni.$on("loading", () => {
	uni.showLoading({
		title: "网络中断,重连中...",
		mask: true,
	})
})
uni.$on("unloading", () => {
	uni.hideLoading()
})

Vue.config.productionTip = false
Vue.prototype.$store = store
Vue.prototype.$request = Request
Vue.prototype.$util = util

Vue.prototype.baseUrl = "http://" + config.url

App.mpType = 'app'

const app = new Vue({
	...App,
	store
})
app.$mount()
// #endif

// #ifdef VUE3
import {
	createSSRApp
} from 'vue'
import App from './App.vue'
export function createApp() {
	const app = createSSRApp(App)
	return {
		app
	}
}
// #endif
